package com.mentalhealthplatform.schoolV1.repository;

import com.mentalhealthplatform.appModels.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
public interface StudentRepositoryV1 extends JpaRepository<Student, UUID> {

    // 根据用户名查找学生，并加载角色信息
    @EntityGraph(attributePaths = "roles")
    Optional<Student> findByUsername(String username);

    // 根据姓名查找学生，并加载角色信息
    @EntityGraph(attributePaths = "roles")
    List<Student> findByNameContaining(String name);

    // 获取所有学生，并加载角色信息
    @Override
    @EntityGraph(attributePaths = "roles")
    List<Student> findAll();

    // 根据ID列表批量删除
    @Transactional
    void deleteAllByIdIn(List<UUID> ids);

    @EntityGraph(attributePaths = "roles")
    Optional<Student> findByNumber(String number);

    @Query("SELECT s FROM Student s WHERE " +
            "( :name IS NULL OR s.name = :name ) AND " +
            "( :major IS NULL OR s.major = :major ) AND " +
            "( :college IS NULL OR s.college = :college ) AND " +
            "( :gender IS NULL OR s.gender = :gender ) AND " +
            "( :startDate IS NULL OR s.birthDate >= :startDate ) AND " +
            "( :endDate IS NULL OR s.birthDate <= :endDate )")
    Page<Student> findByFilters(String name, String major, String college, String gender,
                                LocalDateTime startDate, LocalDateTime endDate, Pageable pageable);
}
